package work.linruchang.util;

import cn.hutool.core.convert.BasicType;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.ReflectUtil;
import com.forte.util.Mock;
import com.forte.util.loader.MethodLoader;
import com.forte.util.mockbean.MockMapBean;
import com.forte.util.utils.MockUtil;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * 增强的{@link MockUtil}
 *
 * @author LinRuChang
 * @version 1.0
 * @date 2022/10/08
 * @since 1.8
 **/
public class EnhanceMockUtil {


    private static Object mockRandomData(String dateType) {
        Class aClass = EnhanceClassUtil.getJdkClass(dateType);
        if (aClass == null) {
            return null;
        }

        boolean isArrayFlag = aClass.isArray();
        Integer arrayElemeCount = RandomUtil.randomInt(1, 10);
        Object[] result = new Object[0];


        // 数组类型
        if (isArrayFlag) {
            aClass = BasicType.wrap(EnhanceClassUtil.getArrayElementClass(aClass));
        } else { // 普通类型
            aClass = BasicType.wrap(aClass);
        }

        //不会有基本类型
        Class elementClass = aClass;

        if (aClass == String.class) {
            result = MockUtil.names(arrayElemeCount);
        } else if (EnhanceClassUtil.isNumberType(aClass)) {
            result = EnhanceRandomUtil.nums(aClass, arrayElemeCount);
        }else if(aClass == Boolean.class) {
            result = EnhanceRandomUtil.bools(arrayElemeCount);
        }else if(EnhanceClassUtil.isDateType(aClass)) {
            result = EnhanceRandomUtil.dates(aClass,arrayElemeCount);
        }else if (aClass == Character.class) {
            result = EnhanceRandomUtil.characters(arrayElemeCount);
        }


        return isArrayFlag?result:EnhanceArrayUtil.getFirst(result);
    }


    /**
     * mock数据生成
     *
     * @param dateType    mock的数据类型
     * @param proertyName mock的字段名
     * @return
     */
    public static Object mockData(String dateType, String proertyName) {
        Class dateTypeClazz = EnhanceClassUtil.getJdkClass(dateType);
        if (dateTypeClazz != null) {
            // 不支持容器类型转换
            if (Collection.class.isAssignableFrom(dateTypeClazz)) {
                return Collections.emptyList();
            } else  {
                return mockRandomData(dateType);
            }
        } else {
            return null;
        }
    }


    public static void main(String[] args) {

        MethodLoader methodLoader = Mock.mockMethodLoader();
        methodLoader.append(ReflectUtil.getMethod(EnhanceMockUtil.class, "testMock"));
        methodLoader.append(ReflectUtil.getMethod(EnhanceMockUtil.class, "mockData", String.class, String.class));
        methodLoader.load();


        Map<String, Object> ruleMap = new HashMap<>();
        ruleMap.put("age1", "@age");
        ruleMap.put("age2", 15);
        ruleMap.put("age3", new Integer[]{1, 2, 3, 4});
        ruleMap.put("name1", "@name");
        ruleMap.put("name2", "@title(2)");
        ruleMap.put("name3", "这是一个名字");

        ruleMap.put("email1", "@email('这是中文')");
        ruleMap.put("email2", "@email('this is english')");
        ruleMap.put("email3", "@email(this is english)");
        ruleMap.put("testMock", "@testMock");
        ruleMap.put("mockData", "@mockData('fdsfds','fdsfdsf5345')");
        // ruleMap.put("email4" , null);

        MockMapBean test = Mock.setResult("test", ruleMap, false);

        test.getObject().entrySet()
                .forEach(Console::log);


    }


}
